<?php

namespace app\modules\log\service;

use Yii;
use app\common\base\CommonService;
use app\modules\log\models\Operation;
use app\helpers\DatetimeHelper;

class OperationService extends CommonService
{
    function __construct()
    {
        // 对父类service(component)初始化，并指定model
        parent::init();
        $this->model = new Operation();
    }
    /**
     * 找到操作名称对应的Id
     */
    public function findId($operationName)
    {
        $info = $this->info([
            'condition' => [
                'operationName' => $operationName,
            ]
        ]);
        if (empty($info)) return parent::error('ERROR_INVALID_OPERATIONNAME', '操作不存在');
        return $info['id'];
    }

    /**
     * 找到操作名称对应的名
     */
    public function findName($operationId)
    {
        $info = $this->info([
            'condition' => [
                'id' => $operationId,
            ]
        ]);
        if (empty($info)) return parent::error('ERROR_INVALID_OPERATIONNAME', '操作不存在');/*新错误还未加入error库里 */
        return $info['operationName'];
    }

    /**
     * 增加操作名
     */
    public function add($operationName)
    {
        $this->model->operationName = $operationName;
        $this->model->save();
        return [
            'id' => $this->model->id,
            'operationName' => $this->model->operationName,
        ];
    }

    /**
     * 删除操作名
     */
    public function delete($operationId)
    {
        $this->delete([
            'condition' => [
                'id' => $operationId,
            ]
        ]);
        return [
            'success' => 1,
        ];
    }

    public function list()
    {
        return $this->lists([
            'fields' => ['*'],
        ]);
    }
}
